package org.maxgroundstation.groundstation.view.overlay;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.text.DecimalFormat;

import org.maxgroundstation.groundstation.GroundStation;
import org.maxgroundstation.groundstation.comm.service.echo.EchoService;

/**
 * An overlay graphic for displaying ping latency.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class PingLatencyOverlay implements Overlay {

    // Data members.
    private GroundStation gs;
    private boolean display;
    private DecimalFormat formatter;
    
    /**
     * Constructor
     * @param gs the ground station.
     */
    public PingLatencyOverlay(GroundStation gs) {
        // Initialize data members.
        this.gs = gs;
        display = true;
        formatter = new DecimalFormat("0.0");
    }
    
    @Override
    public boolean canDisplay() {
        return gs.getCommandSender().isConnected();
    }

    @Override
    public boolean getDisplay() {
        return display;
    }

    @Override
    public String getName() {
        return "Ping Latency";
    }

    @Override
    public void paintOverlay(Graphics g, int panelWidth, int panelHeight) {
        Graphics2D g2d = (Graphics2D) g;
        
        g2d.setColor(Color.GREEN);
        g2d.setFont(new Font(g2d.getFont().getFontName(), Font.BOLD, 10));
        
        // Display ping latency.
        EchoService echoService = getEchoService();
        if (echoService != null) {
            String pingString = "Ping Latency - UDP: " + getUDPPing() + ", TCP: " + 
                    getTCPPing();
            TextDisplayUtil.drawText(pingString, (panelWidth / 2), panelHeight - 25, g);
        }
    }

    @Override
    public void setDisplay(boolean displayOverlay) {
        this.display = displayOverlay;
    }

    /**
     * Gets the echo service.
     * @return echo service.
     */
    private EchoService getEchoService() {
        return (EchoService) gs.getServiceComm().getService(EchoService.SERVICE_ID);
    }
    
    /**
     * Gets the UDP ping latency string.
     * @return ping latency string.
     */
    private String getUDPPing() {
        String result = "No Return";
        EchoService echoService = getEchoService();
        if (echoService != null) {
            if (!echoService.getNoReturnUDP()) 
                result = formatter.format(echoService.getUDPPingLatency()) + " ms";
        }
        return result;
    }
    
    /**
     * Gets the TCP ping latency string.
     * @return ping latency string.
     */
    private String getTCPPing() {
        String result = "No Return";
        EchoService echoService = getEchoService();
        if (echoService != null) {
            if (!echoService.getNoReturnTCP()) 
                result = formatter.format(echoService.getTCPPingLatency()) + " ms";
        }
        return result;
    }
}